package com.sicnu.cheer.generalmodule.util;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

import org.json.JSONObject;

import java.util.Iterator;

/**
 * SharedPreferences操作工具类
 * Created by cheer on 2016/11/7.
 */

public class SharedPreferencesUtil {
    /**
     * 保存当前时间
     *
     * @param activity
     */
    public static void saveTimeByKey(Activity activity, String key) {
        SharedPreferences sharedPreferences = activity.getSharedPreferences(key, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putLong(key, DateUtil.getNowTime());
        editor.commit();
    }
    /**
     * 获取SharedPreferences
     *
     * @param context
     * @return
     */
    public static SharedPreferences getSharedPreferences(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("user", Context.MODE_PRIVATE);
        return sharedPreferences;
    }

    /**
     * 根据key获取对应值
     *
     * @param context
     * @param key
     * @return
     */
    public static String getUserItem(Context context, String key) {
        SharedPreferences sharedPreferences = getSharedPreferences(context);
        String value = sharedPreferences.getString(key, "");
        return value;
    }

    /**
     * 单独保存一项数据
     *
     * @param context
     * @param key
     * @param value
     */
    public static void saveUserItem(Context context, String key, String value) {
        SharedPreferences sharedPreferences = getSharedPreferences(context);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value).commit();
    }
}
